package com.example.config.security.authentication;
import com.alibaba.fastjson.JSON;
import com.example.service.IMenuService;
import com.example.util.JSONAuthentication;
import com.example.vo.ResultVoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 菜单状态校验拦截器
 * @author wsl
 */
@Service("menuAuthenticationFilter")
public class MenuAuthenticationFilter extends OncePerRequestFilter {

    @Autowired
    private IMenuService menuService;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String requestUrl = request.getRequestURI();
        if(menuService.isSysMenu(requestUrl)){//判断是否为系统菜单
            if(menuService.isDisabled(requestUrl)){//判断菜单是否可用
                JSONAuthentication.WriteJSON(request,response, ResultVoEnum.MENU_DISABLED);
                return;
            }
        }
        filterChain.doFilter(request,response);
    }
}
